package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"iot-base/devc/common/loader"
)

func newEdge(c *gin.Context) {
	params := c.Request.URL.Query()
	SN := params.Get("SN")
	fmt.Println("get SN:", SN)
	_, ok := Registtable[SN]
	if ok {
		c.Writer.Write([]byte(fmt.Sprintf("registry=ok\nRegistryCode=%s\nServerVersion=3.0.1\nServerName=ADMS\nPushVersion=3.0.1\nErrorDelay=30\nRequestDelay=5\nTransTimes=00:0014:00\nTransInterval=1\nTransTables=User Transaction\nRealtime=1\nSessionID=%s\nTimeoutSec=10", Registtable[SN].RegistryCode, Registtable[SN].Session)))
	} else {
		sa := false
		var tmpid string
		uid := ""
		edgeid := loader.GetDevs("guard")
		if len(Registtable) == 0 {
			tmpid = edgeid[0].Thing.UID
		} else {
			for _, v := range Registtable {
				for _, id := range edgeid {
					if v.devid == id.Thing.UID {
						sa = true
						break
					}
					uid = id.Thing.UID
				}
				if !sa {
					tmpid = uid
					break
				}
			}
		}
		resc, _ := GetUUID()
		ses, _ := GetUUID()
		Registtable[SN] = status{tmpid, resc, ses}
		c.Writer.Write([]byte("OK"))
	}
	return
}
